﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;

namespace Lesson6_练习题
{
    #region 用泛型实现单例模式
    class SingleBase<T> where T:new()
    {
        private static T instance = new T();  //私有静态成员
        public static T Instance  
        { 
            get
            {
                return instance;
            }
        }
        public SingleBase() { }  //私有构造函数  
        
    }
    class Mgr:SingleBase<Mgr>    //类继承泛型类
    {

    }
    class Text
    {
        private static Text instance = new Text();
        public static Text Instance
        {
            get
            {
                return instance;
            }
        }
        public int value = 10;
    }
    class Text2
    {
        public static Text2 Instance = new Text2();
        public int value;

    }

    #endregion

    #region    利用泛型知识点实现    ArrayList中的增删查改

    class ArrayList<T>
    {
        private T[] array;
        public  int Count;   //当前存入多少数
        public ArrayList()  //定义数组容量
        {
            Count = 0;

            array = new T[16];   //初始化数组
        }
    
    public  void Add(T value)   //思路;判断存入元素的个数是否大于数组实际的长度，如果大于就需要实现扩容操作
    {
       if(Count >= array.Length)   //需要扩容操作
            {
                T[] temp = new T[array.Length * 2];  //扩容操作
                 for(int i = 0;i<temp.Length;i++)
                {
                    temp[i] = array[i];
                }
                 array = temp;     //将扩容后的数组赋值给原来的数组实现换址操作
            }
       else   //正常实现添加逻辑，不需要扩容
       {
                array[Count] = value;
                Count++;

       }
        
    } 

      public int Capacity
        {
            get
            {
                return array.Length;
            }
        }


        public void Remove(T value)   //思路: 判断容器中是否有要指定删除的元素，如果有就删除
        {
            int index = -1;
            for (int i = 0; i < Count; i++)
            {
                if (array[i].Equals(value))
                {
                    index = i;
                    break;
                }
            }
            if(index !=-1)  //遍历执行删除指定元素的操作
            {
                RemoveAt(index);
            }     
        }
        public void RemoveAt(int index)
        {
            if (index < 0||index >=Count)
            {
                Console.WriteLine("索引不合法");
                return;
            }
            else
            {
                for (; index < Count - 1; index++)
                {
                    array[index] = array[index + 1];
                    --Count;
                }
                array[array.Length - 1] = default(T);
            }
        }

        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                {
                    Console.WriteLine("索引不合法");
                    return default(T);
                }
                return array[index];
            }
            set
            {
                if (index < 0 || index >= Count)
                {
                    Console.WriteLine("索引不合法");
                    return;
                }
                array[index] = value;
            }
        }
    
    }



    #endregion
    internal class Program
    {
        static void Main(string[] args)
        {
            Text.Instance.value = 20;

            ArrayList<int>  t1 = new ArrayList<int>();

            t1.Add(1);  //增
            t1.Add(2);
            t1.Add(3);

           
            Console.WriteLine(t1.Count);
            Console.WriteLine("*************");
            Console.WriteLine(t1[1]);  //查
            Console.WriteLine(t1[2]);
            Console.WriteLine("*************");

            t1.RemoveAt(1);    //删
            Console.WriteLine(t1[1]);
            Console.WriteLine("**************");
            Console.WriteLine(t1.Count);



        }
    }
}
